﻿using System.Collections.Generic;
using System.Text;

namespace DatabaseGroup.SSIS.SourceAdapters.Parser
{
    /// <summary>
    /// Stores the underlying text data for a row
    /// </summary>
    internal class RowData : IRowParsingContext
    {
        private readonly List<string> _columnValues = new List<string>();
        private readonly StringBuilder _rowText = new StringBuilder();

        /// <summary>
        /// Get the text for a row
        /// </summary>
        public string RowText
        {
            get
            {
                return _rowText.ToString();
            }
        }

        /// <summary>
        /// Resets all the row stored data
        /// </summary>
        public void ResetRowData()
        {
            _columnValues.Clear();
            _rowText.Length = 0;
        }

        /// <summary>
        /// Adds data to an additional column for the row
        /// </summary>
        /// <param name="value">The data to associate with the column</param>
        public void AddColumnData(string value)
        {
            _columnValues.Add(value);
        }

        /// <summary>
        /// Returns the data stored within a column of the row
        /// </summary>
        /// <param name="columnIndex">The zero-based index of the column</param>
        /// <returns>The column data</returns>
        public string GetColumnData(int columnIndex)
        {
            if (columnIndex < 0 || columnIndex >= _columnValues.Count)
            {
                throw new System.ArgumentException("columnIndex");
            }

            return _columnValues[columnIndex];
        }

        #region IRowParsingContext Members

        /// <summary>
        /// Gets the current number of columns for the row
        /// </summary>
        public int ColumnCount
        {
            get
            {
                return _columnValues.Count;
            }
        }

        /// <summary>
        /// Adds a character to the row data
        /// </summary>
        /// <param name="ch">The character to add</param>
        public void Append(char ch)
        {
            _rowText.Append(ch);
        }

        #endregion
    }
}
